package core;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * This class processes requests to generate business model-based or subject area-based bus matrices.
 * @author danielgalassi@gmail.com
 *
 */
public class MyBusMatrix {

	/**
	 * Loads stylesheets stored within the JAR file
	 * @param resource name of the stylesheet
	 * @return an InputStream reference of the stylesheet
	 */
	private InputStream getInternalResource(String resource) {
		InputStream bundledResource = null;
		try {
			bundledResource = getClass().getClassLoader().getResourceAsStream(resource);
		} catch (Exception e) {
			System.out.println("Stylesheet " + resource + " could not be loaded.");
		}
		return bundledResource;
	}

	public static void main(String[] args) {
		Map<String, String> params = new HashMap<String, String>();
		String xudml = "";
		boolean selectInvoked = false;
		boolean fileInvoked = false;
		for (String arg : args) {
			if (arg.startsWith("-select=")) {
				params.put("MySubjectArea", arg.substring(8));
				selectInvoked = true;
			}
			if (arg.startsWith("-xudml=")) {
				xudml = arg.substring(7);
				fileInvoked = true;
			}
		}
		if (!fileInvoked) {
			System.out.println("Exiting, missing parameters...");
			System.out.println("-xudml=<file>");
			System.out.println("-select=concatenated list of subject areas (optional)");
			return;
		}

		MyBusMatrix me = new MyBusMatrix();
		//business model-based bus matrices
		if (!selectInvoked) {
			//XMLUtils.applyStylesheet("output_20130924.xml", "BusMatrix2.xsl", "output.xml");
			//XMLUtils.applyStylesheet("output.xml", "Output.xsl", "output.html");
			InputStream style1 = me.getInternalResource("stylesheets/BusMatrix2.xsl");
			InputStream html = me.getInternalResource("stylesheets/Output.xsl");
			File temp1 = new File("temp1.xml");
			File results = new File("BusMatrix.html");
			XMLUtils.applyStylesheet(xudml, style1, temp1, null);
			XMLUtils.applyStylesheet(temp1, html, results, null);
			temp1.deleteOnExit();
		}

		//subject area-centric bus matrices
		if (selectInvoked) {
			//stylesheets to transform XML/XUDML file
			InputStream style1 = me.getInternalResource("stylesheets/BusMatrix3.xsl");
			InputStream style2 = me.getInternalResource("stylesheets/BusMatrix4.xsl");
			InputStream style3 = me.getInternalResource("stylesheets/BusMatrix5.xsl");
			InputStream html = me.getInternalResource("stylesheets/Output2.xsl");
			//temporary files
			File temp1 = new File("output2.xml");
			File temp2 = new File("output3.xml");
			File temp3 = new File("output4.xml");
			File results = new File("BusMatrix.html");
			//Formatting XUDML as a subject area-based bus matrix document
			XMLUtils.applyStylesheet(xudml, style1, temp1, params);
			XMLUtils.applyStylesheet(temp1, style2, temp2, null);
			XMLUtils.applyStylesheet(temp2, style3, temp3, null);
			XMLUtils.applyStylesheet(temp3, html, results, null);
			//removing temporary files
			temp1.deleteOnExit();
			temp2.deleteOnExit();
			temp3.deleteOnExit();
		}
	}

}
/*
To generate subject area-centric bus matrices:
-select="Procurement and Spend - Purchase Order Financials;Supplier Product Quality;Order Fulfilment Summary" -xudml=sampleCases/production.xml
or
To generate a business model-centric bus matrix:
-xudml=sampleCases/production.xml 
*/
